package com.rico.kok.config;

import com.rico.kok.tool.MessageUtils;
import com.rico.kok.tool.PostUtils;
import com.rico.kok.tool.RegisterUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class Config implements WebMvcConfigurer {
    //国际化
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocalResolver();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
//        registry.addViewController("/login.html").setViewName("login");
//        registry.addViewController("/register.html").setViewName("register");
    }

    //静态文件路径映射
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取文件的真实路径
        String path = fileDirConfig().getUserPicDir() + "/kok/src/userpic/";
        //static/upload/**是对应resource下工程目录
        registry.addResourceHandler("/kok/src/userpic/**").addResourceLocations("file:" + path);
    }

    //静态文件的读取路径配置
    @Bean
    public FileDirConfig fileDirConfig() {
        //创建类
        FileDirConfig fileDirConfig = new FileDirConfig();
        //创建文件读取类
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置用户头像路径
        fileDirConfig.setUserPicDir(properties.getProperty("config.useDirect"));
        //设置内容内容文件的路径
        fileDirConfig.setPostDir(properties.getProperty("config.postDirect"));
        return fileDirConfig;
    }

    //注册工具类
    @Bean
    public RegisterUtils registerUtils() {
        return new RegisterUtils();
    }

    //内容工具类
    @Bean
    public PostUtils postUtils() {
        return new PostUtils();
    }

    //消息工具类
    @Bean
    public MessageUtils messageUtils(){
        return new MessageUtils();
    }
}
